// 格式化标准时间
export function formatStandardTime(value) {
  if (typeof value !== "object" || !value) return "";
  const dateArr = value.toLocaleDateString().split("/");
  let date = "";

  dateArr.forEach((item, index) => {
    if (item.length === 1) {
      item = 0 + item;
    }
    if (index === dateArr.length - 1) {
      date += item;
    } else {
      date += item + "-";
    }
  });

  return date + " " + value.toTimeString().substr(0, 8);

  // return (
  //   value.toLocaleDateString().replace(/\//g, "-") +
  //   " " +
  //   value.toTimeString().substr(0, 8)
  // );
}

// 获取当前标准时间
export function gettime() {
  const date = new Date(
    new Date().getTime() +
      (parseInt(new Date().getTimezoneOffset() / 60) + 8) * 3600 * 1000
  );
  return formatStandardTime(date);
}

// 处理完整时间与时间
export function formatDateTime(time, date) {
  let dateStr = date.split(" ")[0];
  let dateArr = dateStr.split("-");

  let dateTimeArr = date.split(" ")[1].split(":");

  let timeArr = time.split(":");

  let newHour = parseInt(dateTimeArr[0]) + parseInt(timeArr[0]);
  let newMinute = parseInt(dateTimeArr[1]) + parseInt(timeArr[1]);
  let newSeconds = parseInt(dateTimeArr[2]);

  let newDay = parseInt(dateArr[2]);
  let newMonth = parseInt(dateArr[1]);
  let newYear = parseInt(dateArr[0]);

  if (newMinute >= 60) {
    newHour += parseInt(newMinute / 60);
    newMinute = newMinute % 60;
  }

  if (newHour >= 24) {
    newDay += parseInt(newHour / 24);
    newHour = newHour % 24;
  }

  if (newDay > 31) {
    newMonth += 1;
    newDay = 1;
  }

  if (newMonth > 12) {
    newYear += 1;
    newMonth = 1;
  }

  const result =
    newYear +
    "-" +
    newMonth +
    "-" +
    newDay +
    " " +
    newHour +
    ":" +
    newMinute +
    ":" +
    newSeconds;
  return chengeFormat(result);
}

// 修改时间格式
export function chengeFormat(datetime) {
  let date = datetime.split(" ")[0];
  let time = datetime.split(" ")[1];
  let dateArr = date.split("-");
  let timeArr = time.split(":");
  let newDate = "";
  let newTime = "";

  dateArr.forEach((item, index) => {
    if (item.length === 1) {
      if (index < 2) newDate = newDate + "0" + item + "-";
      else newDate = newDate + "0" + item;
    } else {
      if (index < 2) newDate = newDate + item + "-";
      else newDate = newDate + item;
    }
  });

  timeArr.forEach((item, index) => {
    if (item.length === 1) {
      if (index < 2) newTime = newTime + "0" + item + ":";
      else newTime = newTime + "0" + item;
    } else {
      if (index < 2) newTime = newTime + item + ":";
      else newTime = newTime + item;
    }
  });

  return newDate + " " + newTime;
}

/**
 * 判断结束时间是否大于开始时间
 * @param {*} startTime
 * @param {*} endTime
 */
export function toCompareTimeSize(startTime, endTime) {
  let startDatetimeArr = startTime.split(" ");
  let endDatetimeArr = endTime.split(" ");

  let startDateArr = startDatetimeArr[0].split("-");
  let endDateArr = endDatetimeArr[0].split("-");

  let startTimeArr = startDatetimeArr[1].split(":");
  let endTimeArr = endDatetimeArr[1].split(":");

  const dateEqual =
    parseInt(endDateArr[0]) === parseInt(startDateArr[0]) &&
    parseInt(endDateArr[1]) === parseInt(startDateArr[1]) &&
    parseInt(endDateArr[2]) === parseInt(startDateArr[2]);

  if (parseInt(endDateArr[0]) > parseInt(startDateArr[0])) {
    return true;
  } else if (
    parseInt(endDateArr[0]) === parseInt(startDateArr[0]) &&
    parseInt(endDateArr[1]) > parseInt(startDateArr[1])
  ) {
    return true;
  } else if (
    parseInt(endDateArr[0]) === parseInt(startDateArr[0]) &&
    parseInt(endDateArr[1]) === parseInt(startDateArr[1]) &&
    parseInt(endDateArr[2]) > parseInt(startDateArr[2])
  ) {
    return true;
  } else if (dateEqual && parseInt(endTimeArr[0]) > parseInt(startTimeArr[0])) {
    return true;
  } else if (
    dateEqual &&
    parseInt(endTimeArr[0]) === parseInt(startTimeArr[0]) &&
    parseInt(endTimeArr[1]) > parseInt(startTimeArr[1])
  ) {
    return true;
  } else if (
    dateEqual &&
    parseInt(endTimeArr[0]) === parseInt(startTimeArr[0]) &&
    parseInt(endTimeArr[1]) === parseInt(startTimeArr[1]) &&
    parseInt(endTimeArr[2]) > parseInt(startTimeArr[2])
  ) {
    return true;
  } else {
    return false;
  }
}

/**
 * 计算签到签退时间范围
 *  0 签到
 *  1 签退
 */
export function calculateTimeRange(time, timeRange, type) {
  console.log(time, timeRange);
  let datetimeArr = time.split(" ");
  let dateArr = datetimeArr[0].split("-");
  let timeArr = datetimeArr[1].split(":");

  let seconds = parseInt(timeArr[2]);
  let hour = parseInt(timeArr[0]);
  let minute = parseInt(timeArr[1]);

  let day = dateArr[2];
  let month = dateArr[1];
  let year = dateArr[0];

  if (type === 0) {
    minute = parseInt(timeArr[1]) + parseInt(timeRange);

    console.log(minute);

    if (minute >= 60) {
      hour = parseInt(timeArr[0]) + parseInt(minute / 60);
      minute = minute % 60;
    }

    if (hour >= 24) {
      day = parseInt(dateArr[2]) + 1;
      hour = hour % 60;
    }

    // 这里不严谨，还要判断是几月，什么年
    if (day > 31) {
      month += 1;
      day = 1;
    }

    if (month > 12) {
      year += 1;
      month = 1;
    }
  } else if (type === 1) {
    minute = parseInt(timeArr[1]) - parseInt(timeRange);

    if (minute < 0) {
      hour = parseInt(timeArr[0]) - parseInt(-minute / 60);
      minute = minute % 60;
    }

    if (hour < 0) {
      day = parseInt(dateArr[2]) - 1;
      hour = 24 - (hour % 24);
    }

    if (day <= 0) {
      month -= 1;
      day = 31;
    }

    if (month <= 0) {
      year -= 1;
      month = 12;
    }
  }

  const result =
    year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds;

  return chengeFormat(result);
}

/**
 * 计算时间差
 * @param {String} nowTime 开始时间 2020-1-1 12:00:00
 * @param {String} endTime 开始时间 2020-1-1 12:00:00
 * @returns {Number} 秒
 */
export function timeDifference(nowTime, endTime) {
  console.log(nowTime, endTime);
  let nowDatetimeArr = nowTime.split(" ");
  let endDatetimeArr = endTime.split(" ");

  let nowDateArr = nowDatetimeArr[0].split("-");
  let endDateArr = endDatetimeArr[0].split("-");

  let nowTimeArr = nowDatetimeArr[1].split(":");
  let endTimeArr = endDatetimeArr[1].split(":");

  let year = parseInt(endDateArr[0] - nowDateArr[0]);
  let month = parseInt(endDateArr[1] - nowDateArr[1]);
  let day = parseInt(endDateArr[2] - nowDateArr[2]);
  let hour = parseInt(endTimeArr[0] - nowTimeArr[0]);
  let minute = parseInt(endTimeArr[1] - nowTimeArr[1]);
  let seconds = parseInt(endTimeArr[2] - nowTimeArr[2]);

  // 这里不严谨，还要判断是几月，什么年
  let total =
    year * 12 * 31 * 24 * 3600 +
    month * 31 * 24 * 3600 +
    day * 24 * 3600 +
    hour * 3600 +
    minute * 60 +
    seconds;

  return total;
}

/**
 * 格式化秒
 * @param {Number} value -秒数
 * @param {Number} type -0 12时12分12秒  1- 12 00 00
 * @returns {String}
 */
export function formatSeconds(value, type = 0) {
  let seconds = 0;
  let minute = 0;
  let hour = 0;

  if (value > 60) {
    minute = parseInt(value / 60);
    seconds = value % 60;
  }

  if (minute > 60) {
    hour = parseInt(minute / 60);
    minute = minute % 60;
  }

  if (type === 0)
    return (
      (hour === 0 ? "" : `${hour}小时`) +
      (minute === 0 ? "" : `${minute}分钟`) +
      (seconds === 0 ? "" : `${seconds}秒`)
    );
  else
    return `${String(hour).length === 2 ? hour : "0" + hour}时 ${
      String(minute).length === 2 ? minute : "0" + minute
    }分 ${String(seconds).length === 2 ? seconds : "0" + seconds}秒`;
}

/**
 * 截取完整时间后面的 时分
 * @param {String} dateTime - 日期时间 2022/1/1 10:12:00
 * @return {string} 时分
 */
export function fromatDateTime(dateTime) {
  let time = dateTime.split(" ")[1];
  let timeArr = time.split(":");

  return timeArr[0] + ":" + timeArr[1];
}

/**
 * 判断是否是同一天
 * @param {String} startTime - 开始时间 2020-1-1 12:00:00
 * @param {String} endTime - 结束时间 2020-1-1 12:00:00
 * @param {Number} type - 要返回的日期类型 0-开始日期 1-结束日期
 * @returns {String} 日期 2020-1-1
 */
export function judgeIsSameDay(startTime, endTime, type) {
  let startDate = startTime.split(" ")[0];
  let endDate = endTime.split(" ")[0];
  let startDateArr = startDate.split("-");
  let endDateArr = endDate.split("-");

  if (Number(startDateArr[2]) === Number(endDateArr[2])) {
    return type === 0 ? startDate : "";
  }

  return type === 0 ? startDate : endDate;
}

/**
 * 获取会议的开始时间
 * @param { String } startTime - 开始时间 2020-1-1 12:00:00
 * @returns { String }  今天 | 明天 | 后天 | 日期
 */
export function getMeeingDate(startTime) {
  let startDate = startTime.split(" ")[0];
  let nowDate = gettime().split(" ")[0];

  let startDateArr = startDate.split("-");
  let nowDateArr = nowDate.split("-");

  // 年月相同，日不同
  if (
    Number(startDateArr[0]) === Number(nowDateArr[0]) &&
    Number(startDateArr[1]) === Number(nowDateArr[1])
  ) {
    if (Number(startDateArr[2]) === Number(nowDateArr[2])) return "今天";
    else if (Number(startDateArr[2]) === Number(nowDateArr[2]) + 1)
      return "明天";
    else if (Number(startDateArr[2]) === Number(nowDateArr[2]) + 2)
      return "后天";
    else return startDate;
  }

  // 年相同，日月不同
  if (
    Number(startDateArr[0]) === Number(nowDateArr[0]) &&
    Number(startDateArr[1]) > Number(nowDateArr[1])
  ) {
    if (nowDateArr[2] === startDateArr[2] + 31 - 1) return "明天";
    else if (nowDateArr[2] === startDateArr[2] + 31 - 2) return "后天";
    else return startDate;
  }

  // 年月日都不同
  if (
    Number(startDateArr[0]) > Number(nowDateArr[0]) &&
    Number(startDateArr[1]) < Number(nowDateArr[1])
  ) {
    if (nowDateArr[2] === startDateArr[2] + 31 - 1) return "明天";
    else if (nowDateArr[2] === startDateArr[2] + 31 - 2) return "后天";
    else return startDate;
  }
}
